import { createRouter, createWebHashHistory } from 'vue-router'

const routes = [
  {
    path: '/',
    redirect: '/login',
    meta: {
      isLogin: false
    }
  },
  {
    path: "/404",
    name:'notFound',
    component: import('../views/404View.vue')
  },
  {
    path: "/:catchAll(.*)",
    redirect: {
      name:'notFound'
    },
    component: import('../views/404View.vue')
  },
  {
    path: '/home',
    name: 'home',
    component: () => import('../views/HomeView.vue'),
    children: [
      { path: '/home/welcome', name: 'welcome', component: () => import('../views/Welcome'), meta:{ isLogin: true, roles: ['card'] } },
      { path: '/card/activeCard', name: 'activeCard', component: () => import('../views/Card/Index'), meta:{ isLogin: true, roles: ['card'] } },
	  { path: '/card/newCard', name: 'newCard', component: () => import('../views/Card/newCard'), meta:{ isLogin: true, roles: ['card'] } },
      { path: '/card/cardList', name: 'cardList', component: () => import('../views/Card/List'), meta:{ isLogin: true, roles: ['card'] } },
      { path: '/card/createCard', name: 'createCard', component: () => import('../views/Card/Create'), meta:{ isLogin: true, roles: ['card'] } },
	  { path: '/card/loadingList', name: 'loadingListCard', component: () => import('../views/Card/loadingList'), meta:{ isLogin: true, roles: ['card'] } },
      { path: '/warehouse/loadingGoods', name: 'loadingGoods', component: () => import('../views/Warehouse/Index'), meta:{ isLogin: true, roles: ['warehouse'] } },
      { path: '/warehouse/allGoodsList', name: 'allGoodsList', component: () => import('../views/Warehouse/All'), meta:{ isLogin: true, roles: ['warehouse'] } },
      { path: '/warehouse/loadingList', name: 'loadingList', component: () => import('../views/Warehouse/List'), meta:{ isLogin: true, roles: ['warehouse'] } },
      { path: '/car/deliveryGoods', name: 'deliveryGoods', component: () => import('../views/Car/Index'), meta:{ isLogin: true, roles: ['car'] } },
      { path: '/car/deliveryList', name: 'deliveryList', component: () => import('../views/Car/List'), meta:{ isLogin: true, roles: ['car'] } },
      { path: '/menu/goodsList', name: 'goodsList', component: () => import('../views/Menu/Index'), meta:{ isLogin: true, roles: ['menu'] } },
      { path: '/menu/vipCard', name: 'vipCard', component: () => import('../views/Menu/Vip'), meta:{ isLogin: true, roles: ['menu'] } },
      { path: '/menu/goodMoney', name: 'goodMoney', component: () => import('../views/Menu/Money'), meta:{ isLogin: true, roles: ['menu'] } },
	  { path: '/menu/user', name: 'user', component: () => import('../views/Menu/user'), meta:{ isLogin: true, roles: ['menu'] } },
    ],
    redirect: '/home/welcome'
  },
  {
    path: '/login',
    name: 'login',
    component: () => import('../views/LoginView'),
    meta: {
      isLogin: false
    }
  },
  {
    path: '/index',
    name: 'Index',
    meta: {
      isLogin: false
    }
  }
]

const router = createRouter({
  mode: 'history',
  history: createWebHashHistory(),
  routes
})

router.beforeEach((to, from,next) => {
  // to将要访问的路径
  // from 代表从哪个路径跳转而来
  // next()放行 next('/login')强制跳转
  if (to.path === '/login') return next()
  // 获取token
  const tokenStr = window.sessionStorage.getItem('token')
  if (!tokenStr && to.path !== '/') {
    console.log('reset to login')
    next('/login')
  }
  next()
})

export default router
